<template>
  <transition name="fade" mode="out-in">
    <div class="popup video-popup" v-show="visible" v-if="videoUrl">
      <div class="mask" @click="hideVideo"></div>
      <div class="video-popup-box">
        <video class="i-video fit-img" :src="videoUrl" id="video" preload="true" controls :poster="posterUrl" ></video>
      </div>
      <div class="close-btn hv-opacity flex-center" @click="hideVideo">
        <i class="iconfont icon-cross"></i>
      </div>
    </div>
  </transition>
</template>

<script setup>
  import { ref, onMounted } from 'vue'

  const videoUrl = ref('')
  const visible = ref(false)
  const posterUrl = ref('')

  const show = (url, poster) => {
    videoUrl.value = url
    posterUrl.value = poster
    visible.value = true
    setTimeout(() => {
      const videoDom = document.getElementById('video')
      videoDom.play()
    }, 300)
    
  }

  const hideVideo = () => {
    visible.value = false
    videoUrl.value = ''
    const videoDom = document.getElementById('video')
    videoDom.pause()
  }

  defineExpose({
    show
  });
</script>

<style lang="scss">

  .video-popup {
    position: fixed;
    width: 100%;
    height: 100%;
    left: 0;
    top: 0;
    z-index: 111;
    .mask {
      width: 100%;
      height: 100%;
      position: absolute;
      top: 0;
      left: 0;
      z-index: 1;
      background: rgba(0,0,0,.7);
      transition: all .3s ease-in-out;
      opacity: 1;
    }
    .close-btn {
      position: absolute;
      right: 5rem /* 80/16 */;
      top: 5.3125rem /* 85/16 */;
      width: 3.125rem /* 50/16 */;
      height: 3.125rem /* 50/16 */;
      z-index: 20;
      background: rgba(255,255,255,.5);
      border-radius: 50%;
      .icon-cross {
        color: #FFFFFF;
        font-size: 1.25rem /* 20/16 */;
      }
    }
  }
  .video-popup-box {
    width: 87.5rem /* 1400/16 */;
    height: 49.0625rem /* 785/16 */;
    max-width: 92vw;
    max-height: 92vh;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: #FFFFFF;
    overflow: hidden;
    border-radius: 2px;
    transition: opacity .3s ease-in-out;
    z-index: 9;
  }
</style>